package controlador;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import entidades.ParametrosConexion;
public class CsConexion {
	
	public Connection connection = null;
    public String url = "jdbc:mysql://";
    public String consultasql;
    public Statement instruccion=null;
    public ResultSet resultado=null;
    public DatabaseMetaData dbmd;
    
   
    private static CsConexion CsCon=new CsConexion();
   
       
    
    // Constructor
    private CsConexion() {
    	 
    	try {
			Class.forName("com.mysql.jdbc.Driver");
			ParametrosConexion objPC= new ParametrosConexion();
			objPC.asignarParametros();
			try {
				//connection = DriverManager.getConnection("jdbc:mysql://"+ objPC.getIp()+";databaseName="+objPC.getBasedatos()+"; user="+objPC.getUsuario()+"; password="+objPC.getContrasenia());
				connection = DriverManager.getConnection(url + objPC.getIp()+":"+objPC.getPuerto()+
						"/"+objPC.getBasedatos(),objPC.getUsuario(),objPC.getContrasenia());
				dbmd=connection.getMetaData();
				instruccion=connection.createStatement();
				System.out.print("conexion exitosa");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				System.out.print(e);
				e.printStackTrace();
			}
			
			
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	 
    }
    
    public static CsConexion getConnection(){
    	if (CsCon==null)
    	{
    		CsCon=new CsConexion();
    	}
    	
    	
    	return CsCon;
    }
 
    private void closeConnection() {
            try {
                    if (connection != null)
                            connection.close();
                    connection = null;
            } catch (Exception e) {
                    e.printStackTrace();
            }
    }

}
